export default {
  data() {
    return {
      searchItem: {
        searchKey: "",
      },
      showDevice: false,
      styles: {
        height: "calc(100% - 55px)",
        overflow: "auto",
        paddingBottom: "53px",
        position: "static",
      },
      formData: {},
      typeList: [
        {
          key: 1,
          title: "海康门禁",
        },
        {
          key: 2,
          title: "海康录像主机",
        },
      ],
      columns: [
        {
          title: "设备名称",
          key: "name",
          fixed: "left",
          width: 150,
        },
        {
          title: "序列号",
          key: "serialnumber",
          align: "center",
        },
        {
          title: "设备型号",
          key: "model",
          align: "center",
        },
        {
          title: "IP地址",
          key: "ip",
          align: "center",
        },
        {
          title: "管理员",
          key: "loginId",
          align: "center",
        },
        {
          title: "状态",
          key: "status",
          slot: "status",
          align: "center",
        },
        {
          title: "操作",
          key: "action",
          align: "center",
          width: 180,
          slot: "action",
          fixed: "right",
        },
      ],
      count: 0,
      total: 0,
      page: 1,
      pageSize: 10,
      datalist: [],
    };
  },
  computed: {
    user() {
      return this.$store.state.user;
    },
  },
  methods: {
    addAction() {
      this.formData = {};
      this.showDevice = true;
    },
    edit(index) {
      var model = this.datalist[index];
      this.formData = Object.assign({}, model);
      this.showDevice = true;
    },
    saveInfo() {
      this.$axios({
        url: "/device",
        method: "PUT",
        data: this.formData,
      }).then((res) => {
        if (res.code == 200) {
          this.formData = {};
          this.showDevice = false;
          this.getData();
        } else {
          this.$Message.error(res.msg);
        }
      });
    },
    pageChange(page) {
      this.page = page;
      this.getData();
    },
    pageSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.getData();
    },
    getData() {
      this.$axios({
        url: `/device/list`,
        method: "POST",
        data: {
          page: this.page,
          pageSize: this.pageSize,
          searchKey: this.searchItem.searchKey,
        },
      }).then((res) => {
        if (res.code == 200) {
          this.datalist = res.data.list;
          this.total = res.data.total;
        }
      });
    },
    deleteInfo(index) {
      var that = this;
      var model = this.datalist[index];
      this.$Modal.confirm({
        title: "删除提示",
        content: `确定要删除设备：“${model.name}”么？`,
        onOk: () => {
          this.$axios({
            url: `/device/${model.deviceid}`,
            method: "DELETE",
          }).then((res) => {
            if (res.code == 200) {
              this.$Message.info(res.data);
              that.getData();
            } else {
              this.$Message.error(res.msg);
            }
          });
        },
      });
    },
    notice(index) {
      var model = this.datalist[index];
      this.$axios({
        url: `/device/notice/${model.deviceid}`,
      }).then((res) => {
        if (res.code == 200) {
          this.$Message.info(res.data);
          // that.getData();
        } else {
          this.$Message.error(res.msg);
        }
      });
    },
  },
  created() {
    this.getData();
  },
};
